package com.bizdroid.unutturma.service;

import java.util.ArrayList;
import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.bizdroid.unutturma.R;
import com.bizdroid.unutturma.activity.NotificationActivity;
import com.bizdroid.unutturma.model.ReminderItem;
import com.bizdroid.unutturma.utility.Utility;

public class LocationService extends Service {

	private static final String TAG = "UnutturmaLocationService";

	private Location currentLocation;
	private LocationManager locationManager;
	
	private String ns = Context.NOTIFICATION_SERVICE;
	NotificationManager mNotificationManager;
	private int icon = R.drawable.notification_icon;
	private CharSequence tickerText = "tickerText";
	

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");

		this.locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		
		this.mNotificationManager = (NotificationManager) getSystemService(ns);
		
	}

	@Override
	public void onDestroy() {
		Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
		
		locationManager.removeUpdates(locationListenerGps);
		locationManager.removeUpdates(locationListenerNetwork);
		
		locationManager  = null;
		currentLocation  = null;
		
	}

	@Override
	public void onStart(Intent intent, int startid) {
		Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,0, locationListenerGps);
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0,locationListenerNetwork);
	}

	LocationListener locationListenerGps = new LocationListener() {
		public void onLocationChanged(Location location) {
			gotLocation(location, LocationManager.GPS_PROVIDER);
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	LocationListener locationListenerNetwork = new LocationListener() {
		public void onLocationChanged(Location location) {
			gotLocation(location, LocationManager.NETWORK_PROVIDER);
		}

		public void onProviderDisabled(String provider) {
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	private void gotLocation(Location location, String provider) {
		
		String locationInfo = "Current Location"+"latitude: " + location.getLatitude() + " - longitude: " + location.getLongitude() + " - Provider: "+provider;
		Log.d(TAG, locationInfo);
		
		ArrayList<ReminderItem>  reminderList = Utility.getReminderList(this);
		
		
		
		for (ReminderItem reminderItem : reminderList) {
			float distance = location.distanceTo(reminderItem.getLocation());
			float scope = Float.parseFloat(reminderItem.getScope());
			
			if (distance > scope) {
				long when = System.currentTimeMillis();

				Notification notification = new Notification(icon, tickerText, when);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				Context context = getApplicationContext();
				CharSequence contentTitle = "Unutma!";
				CharSequence contentText = reminderItem.getRemindText();
				Intent notificationIntent = new Intent(this, NotificationActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("key",  reminderItem.getKey());
				System.out.println("*********servicekey:" + reminderItem.getKey());
				bundle.putString("contentMessage",  reminderItem.getRemindText());
				notificationIntent.putExtras(bundle);
				Random random = new Random();
				PendingIntent contentIntent = PendingIntent.getActivity(this, random.nextInt(), notificationIntent, 0);

				notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

				
				

				mNotificationManager.notify(1, notification);
				
				locationManager.removeUpdates(locationListenerGps);
				locationManager.removeUpdates(locationListenerNetwork);
			}
		}
		
		
		Location destLocation = new Location(LocationManager.GPS_PROVIDER);
		destLocation.setLatitude(41.0039);
		destLocation.setLongitude(29.1697);
		
		float distance = location.distanceTo(destLocation);
		
//		Toast.makeText(this, locationInfo, Toast.LENGTH_SHORT).show();
//		Toast.makeText(this, String.valueOf(distance), Toast.LENGTH_SHORT).show();
		
		
		
	}

	public Location getCurrentLocation() {
		return currentLocation;
	}

	public void setCurrentLocation(Location currentLocation) {
		this.currentLocation = currentLocation;
	}

}